package com.juhuixing.gateway.util;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

public class ExceptionHandle {
	public static String getErrorInfoFromException(Exception e) {
        return getString(e);
    }

	public static String getErrorInfoFromException(Throwable t) {
        return getString(t);
    }

    @NotNull
    private static String getString(Throwable t) {
        StringWriter sw = null;
        PrintWriter pw = null;
        try {
            sw = new StringWriter();
            pw = new PrintWriter(sw);
            t.printStackTrace(pw);
            return "\r\n" + sw.toString() + "\r\n";
        } catch (Exception e2) {
            return "ErrorInfoFromException";
        }finally {
        	try {
                if(sw != null){
                    sw.close();
                }
			} catch (IOException e1) {
				e1.printStackTrace();
			}
            if(pw != null){
                pw.close();
            }
		}
    }
}
